/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.html.HTMLDocument;

public class TipButton extends JButton {
	
	public static final int POPUP_WIDTH = 200;
	
	private static final long serialVersionUID = -8341319088467955001L;
	//private JWindow listWindow;
	private JPopupMenu listWindow;
	private JEditorPane htmlTip;
	
	public TipButton() {
		super();
		
		setFocusable(false);
		
		setPreferredSize(new Dimension(40,30));
		setOpaque(false);
		setUI(new TipButtonUI());
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				showPopup();
			}
			@Override
			public void mouseExited(MouseEvent e) {
				hidePopup();
			}
		});
		
		
		htmlTip = new JEditorPane();
		//---- label1 ----
		htmlTip.setContentType("text/html");
		htmlTip.setFont(new Font("Tahoma", Font.PLAIN, 12));
		htmlTip.setEditable(false);
		htmlTip.setOpaque(false);
		String bodyRule = "body { font-family: Tahoma; " +
				"font-size: 12pt; }";
		String aRule = "a { font-family: Tahoma; " +
				"font-size: 12pt; color: #d2ec30; }";
		((HTMLDocument)htmlTip.getDocument()).getStyleSheet().addRule(bodyRule);
		((HTMLDocument)htmlTip.getDocument()).getStyleSheet().addRule(aRule);

		htmlTip.setBorder(new EmptyBorder(10,10,10,10));
		htmlTip.setFocusable(false);
		
		listWindow = new JPopupMenu();
		listWindow.setFocusable(false);
		listWindow.setBorder(BorderFactory.createMatteBorder(
				1, 1, 1, 1, new Color(0x828790)));

		GroupLayout layout = new GroupLayout(listWindow);
		listWindow.setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addComponent(htmlTip, 
							POPUP_WIDTH, 
							POPUP_WIDTH, 
							POPUP_WIDTH))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addComponent(htmlTip))
		);
	}
	
	public void setTipText(String text) {
		htmlTip.setText(text);
		listWindow.doLayout();
	}
	
	private void showPopup() {
		Window window = SwingUtilities.windowForComponent(this);
		if(isShowing() && window != null && window.isActive())
			listWindow.show(this, getWidth() - POPUP_WIDTH, getHeight());
	}
	
	private void hidePopup() {
		listWindow.setVisible(false);
	}
}